//trie 树
#include <iostream>
#include <string.h>
using namespace std;

const int N = 3e6 + 10;
int arr[N][62];
int p[N];
int _index;

int getpos(char ch)
{
    if (ch >= 'a' && ch <= 'z') return ch - 'a';
    if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 26;
    if (ch >= '0' && ch <= '9') return ch - '0' + 52;
}

void insert(string& str)
{
    int cur = 0;
    p[cur]++; //维护经过点的数量
    for (auto& e : str)
    {
        int pos = getpos(e);
        if (arr[cur][pos] == 0) arr[cur][pos] = ++_index;
        cur = arr[cur][pos];
        p[cur]++;
    }
}

int find(string& str)
{
    int cur = 0;
    for (auto& e : str)
    {
        int pos = getpos(e);
        if (arr[cur][pos] == 0) return 0;
        cur = arr[cur][pos];
    }
    return p[cur];
}

int main()
{
    ios::sync_with_stdio(false);
    int T; cin >> T;
    while (T--)
    {
        //清空之前的数据
        for (int i = 0; i < _index; i++)
        {
            for (int j = 0; j < 62; j++) arr[i][j] = 0;
            p[i] = 0;
        }
        _index = 0;

        int n, q; cin >> n >> q;
        while (n--)
        {
            string str; cin >> str; 
            insert(str);
        }

        while (q--)
        {
            string str; cin >> str;
            cout << find(str) << endl;
        }
    }

    return 0;
}

